define(['jquery','common'], function($,common){
    var captcha = {
        //获取验证码
        get: function(element){
            $(document).on("click", element, function(){
                var thisClick = $(this);
                //验证手机号
                var mobileVal  = $(thisClick.data('mobile')).val();
                if(!mobileVal){
                    $(thisClick.data('mobile')).focus();
                    return false;
                }
                var reg = /^(0|86|17951)?(13|14|15|16|17|18|19)[0-9]{9}$/;
                if(mobileVal.match(reg) == null){
                    common.toastMini('请输入正确的手机号');
                    return false;
                }
                //验证图形码
                var captchaVal = $(thisClick.data('captcha')+">input").val();
                if(!captchaVal){
                    $(thisClick.data('captcha')+">input").focus();
                    return false;
                }
                //发送验证码
                captcha.ajax($(this).data('url'),{'mobile': mobileVal,'captcha':captchaVal},$(this));
            });
        },
        //后端请求
        ajax: function(url, data, $el){
            $.post(url, data, function(res){
                if(res.code == 1){
                    captcha.interval($el);
                }else{
                    captcha.imageRefresh($el.data('captcha')+">>img");
                    captcha.imageValue($el.data('captcha')+">input",null);
                    common.toastMini(res.msg);
                }
            });
        },
        //按钮倒计时
        interval: function($selector){
            var second = 60;
            $selector.addClass('disabled');
            $selector.text(second+'秒后重发');
            var interval = setInterval(function () {
                second--;
                $selector.text(second+'秒后重发');
                if(second < 1){
                    $selector.text($selector.data('reset'));
                    $selector.removeClass('disabled');
                    clearInterval(interval);
                }
            }, 1000);
        },
        //监听图形验证码
        image: function(element){
            $(element).css({cursor:'pointer'});
            $(document).on("click", element, function(){
                captcha.imageRefresh(element);
            });
        },
        //图形验证码输入框值
        imageValue:function(element,value){
            $(element).val(value);
        },
        //刷新图形验证码
        imageRefresh: function(element){
            var imgUrl = $(element).attr('src');
            $(element).attr('src', imgUrl);
        }
    };
    return captcha;
});